/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package base;

import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Tal Eisenberg
 */
public class Value extends Module {
    private VarMatcher releaseTrigger;
    private VarMatcher storeTrigger;
    private Variable storedVar;

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (releaseTrigger.matches(in, channel, this)) {
            if (storedVar!=null) {
                sendToOutputs(storedVar, -1);
            } else if (debugFlag) log("Received release message but no value is stored.", MSG.DEBUG);
        } else if (storeTrigger.matches(in, channel, this)) {
            if (debugFlag) log("Storing value: " + in.toString(this), MSG.DEBUG);
            storedVar = in.copyVar();
        } else if (debugFlag) log("Ignoring value: " + in.toString(this), MSG.DEBUG);
    }

    public void setReleaseTrigger(VarMatcher trigger) {
        releaseTrigger = trigger;
    } public VarMatcher getReleaseTrigger() { return releaseTrigger; }

    public void setStoreTrigger(VarMatcher trigger) {
        storeTrigger = trigger;
    } public VarMatcher getStoreTrigger() { return storeTrigger; }
}
